Skip to content

fix(bigquery): handle API-wrapped null values in NULLABLE fields#1037

Open
Srujan rai (Srujan-rai) wants to merge 2 commits into
talkiq:masterfrom
Srujan-rai:master
Open

fix(bigquery): handle API-wrapped null values in NULLABLE fields#1037
Srujan rai (Srujan-rai) wants to merge 2 commits into
talkiq:masterfrom
Srujan-rai:master

Conversation

@Srujan-rai
Copy link
Copy Markdown

BigQuery returns nulls as {'v': None}, not bare None. The old check value is None never matched, causing convert(None) to raise TypeError (eg. int(None)) for NULLABLE fields. Use flatten(value) before the check.

Adds regression test for the {'v': None} API response shape.

Summary

  • BigQuery API returns null values wrapped as {'v': None}, not bare None
    • Old guard value is None never matched → fell through to convert(None)TypeError (eg. int(None)) for any NULLABLE INTEGER/BOOLEAN/FLOAT/etc field
      with a null value
    • Fix: use flatten(value) is None before the null check, consistent with how the rest of parse() already uses flatten()
    • Adds regression test covering the {'v': None} API response shape for all NULLABLE types

Fixes #577

BigQuery returns nulls as {'v': None}, not bare None. The old check
`value is None` never matched, causing convert(None) to raise TypeError
(eg. int(None)) for NULLABLE fields. Use flatten(value) before the check.

Adds regression test for the {'v': None} API response shape.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the parse utility to correctly handle wrapped null values from the BigQuery API by applying the flatten function to nullable fields. A unit test was added to verify this behavior. The reviewer suggested optimizing the implementation by flattening the value once at the beginning of the function to avoid redundant recursive calls and improve performance.

Comment thread bigquery/gcloud/aio/bigquery/utils.py Outdated
Comment on lines +104 to +105
if field['mode'] == 'NULLABLE' and flatten(value) is None:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The flatten(value) function is called multiple times throughout the parse function (see lines 113, 115, 120, and 123). To improve efficiency and avoid redundant recursive traversals, consider flattening the value once and reusing the result. Since flatten is idempotent for already-flattened values, this change is safe and improves performance, especially when processing large query results with many nullable fields.

Suggested change
if field['mode'] == 'NULLABLE' and flatten(value) is None:
return None
value = flatten(value)
if field['mode'] == 'NULLABLE' and value is None:
return None

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied value is now flattened once before all checks. All unit tests pass.

Addresses review feedback: flatten value once before all mode/type
checks instead of calling flatten(value) redundantly on every branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Bigquery API: query_response_to_dict raises exception for nullable values.

1 participant